﻿@model AddressModel
@using Nop.Web.Models.Common;

@if (Model.CountryEnabled && Model.StateProvinceEnabled)
{
    <script type="text/javascript">
        $(function() {
            $("#@Html.FieldIdFor(model => model.CountryId)").change(function() {
                var selectedItem = $(this).val();
                var ddlStates = $("#@Html.FieldIdFor(model => model.StateProvinceId)");
                var statesProgress = $("#states-loading-progress");
                statesProgress.show();
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: "@(Url.RouteUrl("GetStatesByCountryId"))",
                    data: { "countryId": selectedItem, "addEmptyStateIfRequired": "true" },
                    success: function(data) {
                        ddlStates.html('');
                        $.each(data, function(id, option) {
                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                        });
                        statesProgress.hide();
                    },
                    error: function() { //xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve states.');
                        statesProgress.hide();
                    }
                });
            });
        });
        $(function () {
            $("#@Html.FieldIdFor(model => model.StateProvinceId)").change(function () {
                var selectedItem = $(this).val();
                var ddlDistricts = $("#@Html.FieldIdFor(model => model.DistrictId)");
                var districtsProgress = $("#districts-loading-progress");
                districtsProgress.show();
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: "@(Url.RouteUrl("GetDistrictsByStateProvinceId"))",
                    data: { "stateProvinceId": selectedItem, "addEmptyDistrictIfRequired": "true" },
                    success: function (data) {
                        ddlDistricts.html('');
                        $.each(data, function (id, option) {
                            ddlDistricts.append($('<option></option>').val(option.id).html(option.name));
                        });
                        districtsProgress.hide();
                    },
                    error: function () {
                        alert('Failed to retrieve districts.');
                        districtsProgress.hide();
                    }
                });
            });
        });
    </script>
}
@Html.HiddenFor(model => model.Id)
<table>
    <tr>
        <td>
            @Html.LabelFor(model => model.FirstName):
        </td>
        <td>
            @Html.EditorFor(model => model.FirstName)
            @Html.RequiredHint()
            @Html.ValidationMessageFor(model => model.FirstName)
        </td>
    </tr>
    <tr>
        <td>
            @Html.LabelFor(model => model.LastName):
        </td>
        <td>
            @Html.EditorFor(model => model.LastName)
            @Html.RequiredHint()
            @Html.ValidationMessageFor(model => model.LastName)
        </td>
    </tr>
    <tr>
        <td>
            @Html.LabelFor(model => model.Email):
        </td>
        <td>
            @Html.EditorFor(model => model.Email)
            @Html.RequiredHint()
            @Html.ValidationMessageFor(model => model.Email)
        </td>
    </tr>
    @if (Model.CompanyEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.Company):
            </td>
            <td>
                @Html.EditorFor(model => model.Company)
                @if (Model.CompanyRequired)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.Company)
            </td>
        </tr>
    }
    @if (Model.CountryEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.CountryId):
            </td>
            <td>
                @Html.DropDownListFor(model => model.CountryId, Model.AvailableCountries)
                @Html.RequiredHint()
                @Html.ValidationMessageFor(model => model.CountryId)
            </td>
        </tr>
    }

    @if (Model.CountryEnabled && Model.StateProvinceEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.StateProvinceId):
            </td>
            <td>
                @Html.DropDownListFor(model => model.StateProvinceId, Model.AvailableStates)
                <span id="states-loading-progress" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
                @Html.ValidationMessageFor(model => model.StateProvinceId)
            </td>
        </tr>
    
        <tr>
            <td>
                @Html.LabelFor(model => model.DistrictId):
            </td>
            <td>
                @Html.DropDownListFor(model => model.DistrictId, Model.AvailableDistricts)
                <span id="districts-loading-progress" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
                @Html.ValidationMessageFor(model => model.DistrictId)
            </td>
        </tr>
    }

    @if (Model.CityEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.City):
            </td>
            <td>
                @Html.EditorFor(model => model.City)

                @if (Model.CityRequired)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.City)
            </td>
        </tr>
    }
    @if (Model.StreetAddressEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.Address1):
            </td>
            <td>
                @Html.EditorFor(model => model.Address1)
                @if (Model.StreetAddressRequired)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.Address1)
            </td>
        </tr>
    }
    @if (Model.StreetAddress2Enabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.Address2):
            </td>
            <td>
                @Html.EditorFor(model => model.Address2)
                @if (Model.StreetAddress2Required)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.Address2)
            </td>
        </tr>
    }
    @if (Model.ZipPostalCodeEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.ZipPostalCode):
            </td>
            <td>
                @Html.EditorFor(model => model.ZipPostalCode)
                @if (Model.ZipPostalCodeRequired)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.ZipPostalCode)
            </td>
        </tr>
    }
    @if (Model.PhoneEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.PhoneNumber):
            </td>
            <td>
                @Html.EditorFor(model => model.PhoneNumber)
                @if (Model.PhoneRequired)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.PhoneNumber)
            </td>
        </tr>
    }
    @if (Model.FaxEnabled)
    {
        <tr>
            <td>
                @Html.LabelFor(model => model.FaxNumber):
            </td>
            <td>
                @Html.EditorFor(model => model.FaxNumber)
                @if (Model.FaxRequired)
                {
                    @Html.RequiredHint()
                }
                @Html.ValidationMessageFor(model => model.FaxNumber)
            </td>
        </tr>
    }
</table>
